define(function (require, exports, module) {
    var myPub=require('./public');
    var oContain = $('.js_contain');
    var oSignIn = $('.js_signIn');                                  // 签到按钮区域
    var oSignInFrame = $('.js_signInFrame');                        // 签到弹窗
    var oSignInRegister = $('.js_signInRegister');                  // 未登录时弹窗
    var oRecordList = $('.js_recordList');                          // 签到记录列表
    var oSignInActive =$('.js_signInActive');                       // 签到活动规则
    var oLoading = $('.js_loadingBtn');                             // 加载中
    var oNoMoreBtn = $('.js_noMoreBtn');                            // 没有更多了
    var cUrl = '/m/u/1.0/member/checkSignActivityStatus';           // 有无活动接口
    var isSignUrl = '/m/u/1.0/member/checkMemberSignStatus';        // 有没有签到接口
    var signUrl = '/m/u/1.0/member/memberSign';                     // 签到接口
    var signEelUrl = '/m/u/1.0/member/memberSignDetail';            // 签到详情接口
    var signRecordUrl = '/m/u/1.0/member/memberSignLog';            // 签到日志接口

    var slayer_black = 'background-color:rgba(0,0,0,0.5); color:#ffffff; border:none; border-radius: 5px; min-width:50%';
    var localUrl = window.location.href;

    var signInUp = {
        start:0,                                // 开始数据
        limit:20,                               // 每次加载数据的个数
        pagearr:1,                              // 当前页码数
        signInTap: true,                        // 签到按钮防止多次点击
        signIn_timer: null,                     // 签到按钮计时器
        activeId:0,                             // 活动Id
        startTime:0,                            // 活动开始时间
        endTime:0,                              // 活动结束时间
        activeRuler:'',                         // 活动规则
        continueDay:0,                          // 活动持续时间
        bLoadOff:false,                         // 加载开关
        scroll_timer:null,                      // 滚动加载开关

        // 进来页面需要判断当前是否有签到活动
        signInData:function(){
          var _this = this;
            G_JSON_POST(cUrl,{},function (data) {
                var dataItem = data;
                // 有签到活动
                if(dataItem.ret.code == '0'){
                    _this.activeId = dataItem.data.id;
                    _this.startTime = dataItem.data.beginDate;
                    _this.endTime = dataItem.data.endDate;
                    _this.activeRuler = dataItem.data.activityRule;
                    _this.continueDay = dataItem.data.activityContinueDay;
                }
                if(oSignInActive && oSignInActive.length > 0){
                    _this.activeHtml(_this.activeRuler);
                }else{
                    _this.isSignIned();
                }
            })
        },

        // 判断用户是否签到
        isSignIned:function(){
          var _this = this;
            G_JSON_POST(isSignUrl,{'activityId':_this.activeId},function (data) {
                var getData = data;
                var dataItem = data.data;
                var unSignHtml = '';                    // 未签到
                var signHtml = '';                      // 已签到

                if(getData.ret.code == '0'){
                    if(localUrl.indexOf('toMyInfo') != -1){
                        unSignHtml = '<span class="signInDel js_signInDel"></span>';
                        signHtml = '<a href="'+BASE_URL+'/memberSign/toSignInRecord"><span class="signInDel signInDeled"></span></a>';
                    }else{
                        unSignHtml = '<div class="signedBox js_signInDel"></div>';
                        signHtml = '<a href="'+BASE_URL+'/memberSign/toSignInRecord"><div class="signedBox active"></div></a>';
                    }
                    // 用已签到
                    if(dataItem.signStatus == '0'){
                        oSignIn.append(signHtml)
                    }else{
                        oSignIn.append(unSignHtml)
                    }
                }
            })
        },

        // 点击签到按钮请求接口
        getSignData:function(){
          var _this = this;
            // 签到成功
            G_JSON_POST(signUrl,{'activityId':_this.activeId},function (data) {
                var dataTeam = data;
                // 登录过期
                if(dataTeam.ret.code == '00100'){
                    var noLogin = '您的登录已过期，是否重新登录？';
                    var loginUrl = '/h/login/';
                    _this.bindMobileHtml(noLogin,'取消','重新登录',loginUrl);
                    oSignInRegister.show();

                }else if(dataTeam.ret.code == '00128'){
                    // 没有绑定手机号
                    var noBind = '尊敬的用户，您尚未绑定手机，绑定手机后方可参与本次活动，是否立即绑定？';
                    var bindUrl = '/h/login/bindMobile';
                    _this.bindMobileHtml(noBind,'暂不参与','立即绑定',bindUrl);
                    oSignInRegister.show();
                }else if(dataTeam.ret.code == '0'){
                    if(dataTeam.data.signStatus == '0'){
                        // 用户已经签到过了
                        myPub.TipLayerEnd(dataTeam.data.message, slayer_black,function () {
                            window.location.reload()
                        },2);
                    }else if(dataTeam.data.signStatus == '1'){
                        // 用户未签到
                        myPub.TipLayerEnd(dataTeam.data.message, slayer_black,function () {
                            window.location.reload()
                        },2);
                    }else if(dataTeam.data.signStatus == '2'){
                        // 签到成功
                        if(_this.activeId == 0){
                            myPub.TipLayerEnd(dataTeam.data.message, slayer_black,function () {
                                window.location.reload()
                            },2);
                        }else{
                            // 有签到活动
                            _this.signPrize();
                        }

                        if(localUrl.indexOf('toMyInfo') != -1){
                            oSignIn.html('<a href="'+BASE_URL+'/memberSign/toSignInRecord"><span class="signInDel signInDeled"></span></a>');
                        }else{
                            oSignIn.html('<a href="'+BASE_URL+'/memberSign/toSignInRecord"><div class="signedBox active"></div></a>');
                        }
                    }
                }else{
                    console.log('没有签到活动或者签到不成功后')
                }
            });
        },

        // 有签到活动内容
        signPrize:function(){
          var _this = this;
            G_JSON_POST(signEelUrl,{'activityId':_this.activeId},function (data) {
                var dataDel = data;
                if(dataDel.ret.code == '0' && dataDel.data != null){
                    var dataList = dataDel.data;
                    oSignInFrame.show();
                    _this.signInHtml(dataList);
                }
            });
        },

        // 签到日志记录
        recordData:function(){
            var _this = this;
            G_JSON_POST(signRecordUrl,{
                'activityId':_this.activeId,
                'start':_this.start,
                'limit':_this.limit
            },function (data) {
                var dataDel = data;
                if(dataDel.ret.code == '0' && dataDel.data != null){
                    var dataList = dataDel.data;
                    if(dataList.length == 0 && _this.pagearr < 2){
                        _this.loadOff = true;
                        oLoading.hide();
                        oNoMoreBtn.show();
                        return
                    }
                    if(_this.pagearr >= 1){
                        if(dataList.length == 0){
                            _this.loadOff = true;
                            oLoading.hide();
                            oNoMoreBtn.show();
                            return
                        }
                    }
                    //拼接页面
                    _this.recordHtml(dataList);

                    if(dataList.length <  _this.limit) {
                        _this.bLoadOff = true;
                        oLoading.hide();
                        oNoMoreBtn.show();
                    }

                    data = null;
                }else{
                    oLoading.hide();
                    oNoMoreBtn.show();
                    myPub.TipLayer(data.message,slayer_black)
                }
            });
        },

        // 滚动加载
        addScroll:function(){
            var _this = this;
            var minScrHeight = 300;
            oContain.off('scorll');
            oContain.on('scroll',function () {
                var afterScroll = $(this).scrollTop();
                clearTimeout(_this.scroll_timer);
                _this.scroll_timer = setTimeout(function () {
                    if(oContain[0].scrollHeight - afterScroll - oContain.height() < minScrHeight){
                        if(!_this.loadOff){
                            // 滚动底部页数增加 起始数据等于上次结束数据  新的结束等于页数*页码数
                            _this.start = _this.limit*_this.pagearr;
                            _this.pagearr++;
                            _this.recordData();
                        }
                    }
                },150)
            })
        },

        // 点击事件
        signInEvent:function () {
            var _this = this;
            oSignIn.on('click',function (e) {
                if(!_this.signInTap)return;
                _this.signInTap = false;
                var target = e.target;
                if($(target).hasClass('js_signInDel')){
                    _this.getSignData();
                }

                e.stopPropagation();
                clearTimeout(_this.signIn_timer);
                _this.signIn_timer = setTimeout(function () {
                    _this.signInTap = true;
                },300)
            });

            // 点击阴影关闭弹窗
            oSignInFrame.on('click',function (e) {
                var target = e.target;
                if($(target).hasClass('js_siginInShadow')){
                    oSignInFrame.hide();
                    window.location.reload();
                }
                e.stopPropagation();
            });

            // 暂不参与
            oSignInRegister.on('click',function (e) {
                var target = e.target;
                if($(target).hasClass('js_signInRegisterClose')){
                    oSignInRegister.hide()
                }
                e.stopPropagation();
            })

        },

        // 拼接弹窗
        signInHtml:function (data) {
          var _this = this;
          var thtml = '';
          var PrizeHtml = '';
          var starTime = _this.setTimeNow(_this.startTime*1,'mid');
          var endTime = _this.setTimeNow(_this.endTime*1,'short');
            if(data && data.length>0){
                var typeClassName = '';     // 判断几个奖品
                if(data.length == 1){
                    typeClassName = 'giftOne'
                }else if(data.length == 2){
                    typeClassName = 'giftTwo'
                }else if(data.length >= 3){
                    typeClassName = 'giftThreeMore'
                }
                for(var i = 0;i<data.length;i++){
                    var recivedtml = '';        // 已经领取
                    var reciveFail = '';        // 领取优惠券失败
                    var reciveImg = '';         // 图片
                    var coverHtml = '';         // 蒙上白板

                    if(data[i].imgPath && data[i].imgPath!= ''){
                        if(data[i].signStatus == "0"){
                            coverHtml = '<span class="coverBg"></span>';
                        }
                        reciveImg = '<img src="'+HTTP_IMAGE_URL+data[i].imgPath+'" alt="">'
                    }else{
                        reciveImg = '<img src="'+BASE_URL+'/static_v4/image/myInfo/signInBox.png'+'" alt="">'
                    }

                    if(data[i].signStatus == "1"){
                        recivedtml =
                            '<span class="giftRecevied">'+
                            '   <img src="'+BASE_URL+'/static_v4/image/myInfo/giftRecevie.png" alt="">'+
                            '</span>';
                    }else if(data[i].signStatus == "2"){
                        reciveFail =
                            '<span class="receiveFail">'+
                            '   <img src="'+BASE_URL+'/static_v4/image/myInfo/receiveFail.png" alt="">'+
                            '</span>';
                    }

                    PrizeHtml +=
                        '<li class="'+typeClassName+'">'+
                        '   <span class="giftAreaImg giftAreaDel">'+
                                recivedtml+
                                reciveFail+
                                coverHtml+
                                reciveImg+
                        '   </span>'+
                        '   <span class="giftAreaTime giftAreaDel">连续'+data[i].activityDay+'天</span>'+
                        '</li>';
                }
            }
            thtml =
                '<div class="js_siginInShadow siginInShadow"></div>'+
                '<div class="signInArea VBox">'+
                '   <span class="giftRibbon">'+
                '       <img src="'+BASE_URL+'/static_v4/image/myInfo/giftRibbon.png" alt="">'+
                '   </span>'+
                '   <span class="giftPacket">'+
                '       <img src="'+BASE_URL+'/static_v4/image/myInfo/giftPacket.png" alt="">'+
                '   </span>'+
                '   <div class="signInTit">'+
                '       <span class="signInTitImg">'+
                '           <img src="'+BASE_URL+'/static_v4/image/myInfo/signInTit11.png" alt="">'+
                '       </span>'+
                '   </div>'+
                '   <div class="signInBody Bflex1 VBox">'+
                '       <div class="giftArea">'+
                '           <ul class="clearfix">'+
                                PrizeHtml+
                '           </ul>'+
                '       </div>'+
                '   </div>'+
                '   <div class="signInBottom">'+
                '       <p class="signInTimer">'+
                '           <span>活动时间'+starTime+'-'+endTime+'查看</span>'+
                '           <a href="'+BASE_URL+'/memberSign/toSignInActive">活动规则</a>'+
                '       </p>'+
                '       <div class="signInRecordArea">'+
                '           <a href="'+BASE_URL+'/memberSign/toSignInRecord">'+
                '               <span class="signInRecord">'+
                '                   <img src="'+BASE_URL+'/static_v4/image/myInfo/signInRecord.png" alt="">'+
                '               </span>'+
                '           </a>'+
                '       </div>'+
                '   </div>'+
                '</div>';
            oSignInFrame.append(thtml);
        },

        // 签到用户尚未绑定手机号码或者未登录
        bindMobileHtml:function (con,noBtn,YBtn,Url) {
          var _this = this;
          var bindHtml = '';
            bindHtml =
                '   <div class="siginInShadow"></div>'+
                '   <div class="signInRegisterArea">'+
                '       <div class="signInRegisterBox">'+
                '           <div class="signInRegisterTip">'+
                                con+
                '           </div>'+
                '           <div class="signInRegisterBot flexBox spBe">'+
                '               <div class="signInRegisterClose js_signInRegisterClose">'+noBtn+'</div>'+
                '               <div class="signInRegisterIn"><a href='+BASE_URL+Url+'>'+YBtn+'</a></div>'+
                '           </div>'+
                '       </div>'+
                '   </div>';
            oSignInRegister.append(bindHtml);
        },

        // 签到记录Html
        recordHtml:function(data){
          var _this = this;
          var thtml = '';
          for(var i = 0;i<data.length;i++){
              thtml+=
                  '<li class="signInRecordDel flexBox spBe BoxCenter">'+
                  '     <div class="signInRecordTime">'+_this.setTimeNow(data[i].create_date)+'</div>'+
                  '     <div>'+data[i].prize+'</div>'+
                  '</li>';
          }
            oRecordList.append(thtml);
        },

        // 活动规则Html
        activeHtml:function(data){
          var _this = this;
          var sendData = data;
          var thtml = '';
          var dataItem = [];
          if(sendData && sendData.indexOf('\r\n') != -1){
              dataItem = sendData.split('\r\n')
          }else{
              dataItem = [sendData];
          }
          for(var i = 0;i<dataItem.length;i++){
              dataItem[i] = dataItem[i].replace(/\s/g,'');
              if(dataItem[i].length > 0){
                  thtml +=
                      '<p>'+dataItem[i]+'</p>'
              }
          }
          oSignInActive.append(thtml)
        },

        // 时间戳转化时间
        setTimeNow:function (now,timeLength) {
            var _this = this;
            var dateNow = new Date(now*1);
            var year = dateNow.getFullYear();
            var month = dateNow.getMonth()+1;
            var date = dateNow.getDate();
            var hour = dateNow.getHours();
            var minute = dateNow.getMinutes();
            month = month<10?'0'+month:month;
            date = date<10?'0'+date:date;
            hour = hour<10?'0'+hour:hour;
            minute = minute<10?'0'+minute:minute;
            if(timeLength == 'short'){
                return month+"."+date;
            }else if(timeLength == 'mid'){
                return year+"."+month+"."+date;
            }else{
                return year+"."+month+"."+date+"  "+hour+':'+minute;
            }
        }
    };
    if(oRecordList && oRecordList.length > 0){
        signInUp.recordData();
        signInUp.addScroll();
    }else{
        signInUp.signInData();
        signInUp.signInEvent();
    }


});